package com.pms.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.pms.pojo.Post;
import com.pms.service.JobService;

import utils.JsonUtils;

@Controller
@RequestMapping("/organization")
public class JobController {
	
	@Autowired
	private JobService jobService;
	
	@RequestMapping(value="/job", method=RequestMethod.GET)
	public String getJobList(Model model) {
		List<Post> jobList = jobService.getJobList();
		
		model.addAttribute("jobList", jobList);
		
		return "organization/job";
		
	}
	
	@RequestMapping(value="/job-search", method=RequestMethod.POST)
	public String searchJob(Model model, Post job) {
		System.out.println("job="+JsonUtils.objectToJson(job));
		if (job.getPostName() == "") {
			job.setPostName(null);
		}
		/*if (job.getPostType() == "") {
			job.setPostType(null);
		}*/
		System.out.println("job="+JsonUtils.objectToJson(job));
		List<Post> jobList = new ArrayList<>();
		System.out.println("jobList="+JsonUtils.objectToJson(jobList));
		/*//
		switch (type) {
			case "postId":
				int postId = Integer.parseInt(condition);
				Post job = jobService.getJobByPostId(postId);
				jobList.add(job);
				break;
			case "postName":
				jobList = jobService.getJobByPostName(condition);
				break;
			case "postType":
				//查询postName的postId
				jobList = jobService.getJobByPostType(1);
				break;
			default:
				break;
		}*/
		jobList = jobService.getJobListByCondition(job);
		model.addAttribute("jobList", jobList);
		return "organization/job";
		
	}
	
	
	@RequestMapping(value="/job-add", method=RequestMethod.GET)
	public String addPostPage() {
		return "organization/job-add";
	}
	
	
	@RequestMapping(value="/job-insert", method=RequestMethod.POST)
	public void insertJob(Post job, HttpServletResponse response) {
		System.out.println("job="+JsonUtils.objectToJson(job));
		jobService.insertPost(job);
		try {
			response.sendRedirect("job");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	@RequestMapping(value="/job-edit", method=RequestMethod.GET)
	@ResponseBody
	public Post updatePostPage(Model model, int postId) {
		Post job = jobService.getJobByPostId(postId);
		return job;
		
	}
	
	@RequestMapping(value="/job-update", method=RequestMethod.POST)
	@ResponseBody
	public String updateJob(Post job, HttpServletResponse response) {
		System.out.println("job="+JsonUtils.objectToJson(job));
		jobService.updateJob(job);
		return "success";
		
	}
	
	@RequestMapping(value="/job-del", method=RequestMethod.GET)
	@ResponseBody
	public String deleteJob(int postId) {
		jobService.deleteJob(postId);
		
		return "success";
		
	}
	
}
